/*
 * @Author: 107w 8801453+lu-shangru@user.noreply.gitee.com
 * @Date: 2023-04-11 16:51:46
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-05-06 11:23:42
 * @Description:
 *
 * Copyright (c) 2023 by 107w, All Rights Reserved.
 */
// vuex的权限模块
import Layout from '@/layout'
import Cookies from 'js-cookie'
// import Login from '@/views/login/index.vue'
import { constantRoutes,
  // asyncRoutes,
  indexRoute } from '@/router'
// import flatMap from 'lodash/flatMap'
// vuex中的permission模块用来存放当前的 静态路由 + 当前用户的 权限路由
const state = {
  routes: constantRoutes // 所有人默认拥有静态路由
}
const mutations = {
  // newRoutes可以认为是 用户登录 通过权限所得到的动态路由的部分
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  filterRoutes(context, menus = []) {
    let routes = updateComponentToUser(menus)
    let firstMenuName = ''
    if (routes[0]) {
      firstMenuName = routes[0].name ?? 'default'
    } else {
      firstMenuName = 'systems'
    }
    routes.push(indexRoute)
    const access_token = Cookies.get('access_token')
    if (access_token && !window.sessionStorage.getItem('menuGroupUUID')) {
      routes = appendSystemsRoute(routes)
    }
    routes = formatRedirect(routes)
    indexRoute.redirect = '/' + firstMenuName
    context.commit('setRoutes', routes)
    return routes
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
// 动态定位父菜单重定向
function formatRedirect(routes) {
  return routes.map(item => {
    if (item.redirect && item.children && item.children.length > 0) {
      const firstChildPath = item.children[0].name
      item.redirect = `/${item.name}/${firstChildPath}`
    }
    return item
  })
}

// 定义一个递归函数，用于遍历并修改树结构中每一项属性
function updateComponentToUser(tree) {
  if (!tree) return []
  return tree.map(node => {
    const pathViews = node.url
    const newNode = {
      path: node.parentId === 0 ? '/' + node.uuid ?? node.value : node.uuid ?? node.value,
      component: node.parentId === 0 ? Layout : node.url ? () => Promise.resolve().then(() => require(`@/views/${pathViews}`)) : () => import('@/views/routerDocument'),
      name: node.uuid ?? node.value,
      meta: {
        title: node.menuName,
        icon: node.uuid ?? node.value
      }
      // children: node.menuSvoList || []
    } // 创建一个新节点，修改component属性为'user'
    if (node.menuSvoList && node.menuSvoList.length > 0) {
      newNode.children = updateComponentToUser(node.menuSvoList) // 递归遍历子节点
    }
    return newNode || {}
  })
}

function appendSystemsRoute(routes) {
  // 判断是否登录， 登录则添加菜单组编辑，没有登录则只展示接口菜单
  routes.unshift(
    {
      path: '/systems',
      component: Layout,
      children: [
        {
          path: '',
          name: 'systems',
          component: () => import('@/views/documentManage/systems'),
          meta: { title: '系统菜单', icon: 'asrcharg_bill' }
        }
      ]
    }
  )
  return routes
}

